package com.heima.article.service.impl;

import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.JSONArray;
import com.baomidou.mybatisplus.core.toolkit.Wrappers;
import com.heima.article.mapper.ApArticleConfigMapper;
import com.heima.article.mapper.ApArticleContentMapper;
import com.heima.article.mapper.ApArticleMapper;
import com.heima.article.service.ArticleFreemarkerService;
import com.heima.article.service.ArticleHomeService;
import com.heima.common.constans.ArticleConstants;
import com.heima.model.article.dtos.ArticleDto;
import com.heima.model.article.dtos.ArticleHomeDto;
import com.heima.model.article.dtos.HotArticleVo;
import com.heima.model.article.pojos.ApArticle;
import com.heima.model.article.pojos.ApArticleConfig;
import com.heima.model.article.pojos.ApArticleContent;
import com.heima.model.common.dtos.ResponseResult;
import com.heima.model.common.enums.AppHttpCodeEnum;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.redis.core.StringRedisTemplate;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import javax.annotation.Resource;
import java.util.Date;
import java.util.List;

@Service
public class ArticleHomeServiceImpl implements ArticleHomeService {

    @Resource
    private ApArticleMapper apArticleMapper;

    @Resource
    private ApArticleContentMapper apArticleContentMapper;

    @Resource
    private ApArticleConfigMapper apArticleConfigMapper;

    @Resource
    private ArticleFreemarkerService articleFreemarkerService;

    @Autowired
    private StringRedisTemplate redisTemplate;
    /**
     * 定义一个service接口即可实现首页，上拉，下拉
     *
     * @param dto
     * type表示类型，==1表示上拉，更多，==2表示下拉，最新
     */
    @Override
    public ResponseResult load(ArticleHomeDto dto,Short type) {
        //1.参数校验
        if(dto.getTag()==null){
            dto.setTag(ArticleConstants.DEFAULT_TAG);
        }
        if(dto.getSize()==null){
            dto.setSize(10);
        }
        if(dto.getMaxBehotTime()==null){
            dto.setMaxBehotTime(new Date());
        }
        if(dto.getMinBehotTime()==null){
            dto.setMinBehotTime(new Date());
        }
        if(type==null){
            type= ArticleConstants.LOADTYPE_LOAD_MORE;
        }
        //2.调用mapper接口
        List<ApArticle> apArticles = apArticleMapper.loadArticleList(dto, type);

        //3.返回数据
        return ResponseResult.okResult(apArticles);
    }



    /**
     * 保存app端文章三剑客的操作
     *
     * @param dto
     * @return
     */
    @Override
    @Transactional
    public ResponseResult save(ArticleDto dto) {
        //1.参数校验
        if(dto==null || dto.getContent()==null){
            return ResponseResult.errorResult(AppHttpCodeEnum.PARAM_INVALID);
        }

        ApArticle aparticle=new ApArticle();
        BeanUtils.copyProperties(dto,aparticle);
        aparticle.setLikes(0);
        aparticle.setViews(0);
        aparticle.setComment(0);
        aparticle.setCollection(0);
        //2.判断是否是保存还是修改
        if(dto.getId()==null){
            //3.如果是保存文章
            //保存ap_article
            apArticleMapper.insert(aparticle);//这步之后，我们就应该在aparticle实体类中能够获取到主键id

            //保存ap_article_config
            ApArticleConfig apArticleConfig=new ApArticleConfig(aparticle.getId());
            apArticleConfigMapper.insert(apArticleConfig);

            //保存ap_article_content
            ApArticleContent aparticleContent=new ApArticleContent();
            aparticleContent.setContent(dto.getContent());
            aparticleContent.setArticleId(aparticle.getId());
            apArticleContentMapper.insert(aparticleContent);
        }else {
            //4.如果是修改文章
            //修改文章ap_article
            apArticleMapper.updateById(aparticle);
            //修改ap_article_content
            ApArticleContent apArticleContent = apArticleContentMapper.selectOne(Wrappers.<ApArticleContent>lambdaQuery().eq(ApArticleContent::getArticleId, aparticle.getId()));
            if(apArticleContent!=null){
                apArticleContent.setContent(dto.getContent());
                apArticleContentMapper.updateById(apArticleContent);
            }
        }

        //异步调用生成静态文件html
        articleFreemarkerService.buildArticleToMinIO(aparticle,dto.getContent());



        return ResponseResult.okResult(aparticle.getId());
    }


    /**
     * 从redis中获取热点数据
     *
     * @param dto
     *
     */
    @Override
    public ResponseResult load2(ArticleHomeDto dto) {
        //1.声明redis的key前缀
        String key=ArticleConstants.HOT_ARTICLE_FIRST_PAGE+dto.getTag();
        String hotArticleVoListStr = redisTemplate.opsForValue().get(key);
        List<HotArticleVo> hotArticleVoList = JSONArray.parseArray(hotArticleVoListStr, HotArticleVo.class);
        return ResponseResult.okResult(hotArticleVoList);
    }
}
